﻿// Singleton to keep a dictionary of static validator instances indexed by their aliases
define([
    'Wjs/core/typeBuilder'
],
function (typeBuilder) {
    'use strict';

    return typeBuilder.defineOnce({

        type: 'validatorManager',

        internal: {

            'static': true,

            construct: function (cfg) {

                this.validators = {};
            }
        },

        // Registers the instance of the validator to allow its retrieval anywhere by its alias
        register: function (validator) {
            var alias = validator.alias;

            if (!alias) {
                return; // Must have an alias to register it
            }

            if (this.validators[alias]) { // Check for unique ids of the registered validators
                throw "Validator of alias:'" + alias + "' is already registered'";
            }

            this.validators[alias] = validator;
        },

        // Removes a validator from the hash of validators
        remove: function (validator) {
            var alias = validator.alias;

            if (this.validators[alias]) { // Check for unique ids of the registered validators
                delete this.validators[alias];
            }
        },

        // Retrieves the validator by its alias
        get: function (alias) {
            return this.validators[alias];
        }
    });

});